package logic

import (
	"context"
	"fmt"
	"wechat/models/mysqlinit"

	"wechat/Wxrpc/internal/svc"
	"wechat/Wxrpc/wxrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LoginLogic) Login(in *wxrpc.LoginRequest) (*wxrpc.LoginResponse, error) {
	// todo: add your logic here and delete this line
	var login mysqlinit.User

	mysqlinit.DB.Debug().Where("account=?", in.Account).Find(&login)

	if login.ID == 0 {
		return nil, fmt.Errorf("当前账户不存在,请重新输入")
	}
	if login.Password != in.Password {
		return nil, fmt.Errorf("密码错误,请重新输入")
	}
	return &wxrpc.LoginResponse{
		Id: int64(login.ID),
	}, nil
}
